//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.xinqi.common.mybatis.config;

import com.xinqi.common.mybatis.annotation.JsonTypeBeanRegistrar;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.executor.resultset.DefaultResultSetHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Properties;

@Intercepts({
    @Signature(type = StatementHandler.class, method = "prepare", args = {Collection.class, Integer.class})})
public class JsonPlugin implements Interceptor {

    private JsonTypeBeanRegistrar.JsonTypeCustomizer jsonTypeCustomizer;

    public JsonTypeBeanRegistrar.JsonTypeCustomizer getJsonTypeCustomizer() {
        return jsonTypeCustomizer;
    }

    public void setJsonTypeCustomizer(JsonTypeBeanRegistrar.JsonTypeCustomizer jsonTypeCustomizer) {
        this.jsonTypeCustomizer = jsonTypeCustomizer;
    }

    public JsonPlugin() {
    }

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if (target instanceof DefaultResultSetHandler) {
            DefaultResultSetHandler resultSetHandler = (DefaultResultSetHandler) target;
            try {
                Field executor = resultSetHandler.getClass().getDeclaredField("executor");
                executor.setAccessible(true);
                Field configuration = resultSetHandler.getClass().getDeclaredField("configuration");
                configuration.setAccessible(true);
                Field mappedStatement = resultSetHandler.getClass().getDeclaredField("mappedStatement");
                mappedStatement.setAccessible(true);
                Field rowBounds = resultSetHandler.getClass().getDeclaredField("rowBounds");
                rowBounds.setAccessible(true);
                Field parameterHandler = resultSetHandler.getClass().getDeclaredField("parameterHandler");
                parameterHandler.setAccessible(true);
                Field boundSql = resultSetHandler.getClass().getDeclaredField("boundSql");
                boundSql.setAccessible(true);
                Field typeHandlerRegistry = resultSetHandler.getClass().getDeclaredField("typeHandlerRegistry");
                typeHandlerRegistry.setAccessible(true);
                Field objectFactory = resultSetHandler.getClass().getDeclaredField("objectFactory");
                objectFactory.setAccessible(true);
                Field reflectorFactory = resultSetHandler.getClass().getDeclaredField("reflectorFactory");
                reflectorFactory.setAccessible(true);
                Field resultHandler = resultSetHandler.getClass().getDeclaredField("resultHandler");
                resultHandler.setAccessible(true);
                return new MyResultSetHandler((Executor) executor.get(target),
                    (MappedStatement) mappedStatement.get(target), (ParameterHandler) parameterHandler.get(target),
                    (ResultHandler<?>) resultHandler.get(target), (BoundSql) boundSql.get(target),
                    (RowBounds) rowBounds.get(target), jsonTypeCustomizer);
            } catch (Exception var13) {
                var13.printStackTrace();
            }
        }

        return target;
    }

    @Override
    public void setProperties(Properties properties) {
    }

}
